//
//  ElecCheckVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/11/27.
//
// 【首页-电子台账】电子台账-待确认/查看

import Foundation
import UIKit

class ElecCheckVC: SubLevelViewController {
    let checkV = ElecCheckView()
    
    var modal: CheckHsModal!
        
    init(withModal _modal: CheckHsModal) {
        super.init(nibName: nil, bundle: nil)
        self.modal = _modal
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "电子台账"
        view.backgroundColor = .bg
        navigationController?.navBarStyle(.white)
        
        if deptName == nil {
            deptName = BSUser.currentUser.deptName
            deptId = BSUser.currentUser.deptId
        }
        
        setupUI()
        checkV.reloadUI(withModal: modal)
    }
    
    // MARK: - Actions
    
    // 确认
    @objc func confirmTapped() {
        // 1 确认 2 取消 3驳回
        view.showToastActivity()
        API.changeStatus(withParam: ChangeStatusParam(hsId: String(modal.info?.hsId ?? -1), status: "1")) { responseModel in
            self.view.hideToastActivity()
            if responseModel.errorCode == .Success {
                self.view.showToast(witMsg: responseModel.errorMessage)
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    // 取消
    @objc func cancelTapped() {
        // 1 确认 2 取消 3驳回
        view.showToastActivity()
        API.changeStatus(withParam: ChangeStatusParam(hsId: String(modal.info?.hsId ?? -1), status: "2")) { responseModel in
            self.view.hideToastActivity()
            if responseModel.errorCode == .Success {
                self.view.showToast(witMsg: responseModel.errorMessage)
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    // 驳回
    @objc func rejectTapped() {
        // 1 确认 2 取消 3驳回
        view.showToastActivity()
        API.changeStatus(withParam: ChangeStatusParam(hsId: String(modal.info?.hsId ?? -1), status: "3")) { responseModel in
            self.view.hideToastActivity()
            if responseModel.errorCode == .Success {
                self.view.showToast(witMsg: responseModel.errorMessage)
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    // 继续填入
    @objc func nextTapped() {
        let type: ElecType = ElecType(rawValue: modal.info?.tzlx ?? "")!
        switch type {
        case .CashBox:
            let vc = ReceiveSenCashBoxVC(withModal: StandingBookTypeModal(typeValue: modal.info?.tzlx), checkModal: modal.info)
            vc.confirmSuccessWith = { () -> () in
                self.navigationController?.popViewController(animated: true)
            }
            navigationController?.pushViewController(vc, animated: true)
            break
        case .SelfCheck:
            let vc = SelfCheckExcelVC(withModal: StandingBookTypeModal(typeValue: modal.info?.tzlx), checkModal: modal.info)
            vc.confirmSuccessWith = { () -> () in
                self.navigationController?.popViewController(animated: true)
            }
            navigationController?.pushViewController(vc, animated: true)
            break
        case .InfraredArm:
            let vc = InfraredArmVC(withModal: StandingBookTypeModal(typeValue: modal.info?.tzlx), checkModal: modal.info)
            vc.confirmSuccessWith = { () -> () in
                self.navigationController?.popViewController(animated: true)
            }
            navigationController?.pushViewController(vc, animated: true)
            break
        default: break;
        }
    }
    
    // MARK: - Setup
    func setupUI() {
        checkV.submitBtn.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside)
        checkV.cancelBtn.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside)
        checkV.nextBtn.addTarget(self, action: #selector(nextTapped), for: .touchUpInside)
        checkV.rejectBtn.addTarget(self, action: #selector(rejectTapped), for: .touchUpInside)
        view.addSubview(checkV)
        checkV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}
